import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import {
  getPermissionList,
  getPermissionById,
  createPermission,
  updatePermission,
  deletePermission,
} from '@/api/permission'

const initialState = {
  permissionList: [],
  currentPermission: null,
  loading: false,
  error: null,
}

// 获取权限列表
export const fetchPermissionList = createAsyncThunk(
  'permission/fetchPermissionList',
  async (_, { rejectWithValue }) => {
    try {
      const data = await getPermissionList()
      return data
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 获取权限详情
export const fetchPermissionById = createAsyncThunk(
  'permission/fetchPermissionById',
  async (id, { rejectWithValue }) => {
    try {
      const data = await getPermissionById(id)
      return data
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 创建权限
export const addPermission = createAsyncThunk(
  'permission/addPermission',
  async (data, { rejectWithValue }) => {
    try {
      const result = await createPermission(data)
      return result
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 更新权限
export const modifyPermission = createAsyncThunk(
  'permission/modifyPermission',
  async ({ id, data }, { rejectWithValue }) => {
    try {
      const result = await updatePermission(id, data)
      return result
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 删除权限
export const removePermission = createAsyncThunk(
  'permission/removePermission',
  async (id, { rejectWithValue }) => {
    try {
      const data = await deletePermission(id)
      return { id, data }
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

const permissionSlice = createSlice({
  name: 'permission',
  initialState,
  reducers: {
    clearError: (state) => {
      state.error = null
    },
    clearCurrentPermission: (state) => {
      state.currentPermission = null
    },
  },
  extraReducers: (builder) => {
    builder
      // 获取权限列表
      .addCase(fetchPermissionList.pending, (state) => {
        state.loading = true
        state.error = null
      })
      .addCase(fetchPermissionList.fulfilled, (state, action) => {
        state.loading = false
        state.permissionList = action.payload.data || action.payload
      })
      .addCase(fetchPermissionList.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 获取权限详情
      .addCase(fetchPermissionById.pending, (state) => {
        state.loading = true
      })
      .addCase(fetchPermissionById.fulfilled, (state, action) => {
        state.loading = false
        state.currentPermission = action.payload.data || action.payload
      })
      .addCase(fetchPermissionById.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 创建权限
      .addCase(addPermission.pending, (state) => {
        state.loading = true
      })
      .addCase(addPermission.fulfilled, (state) => {
        state.loading = false
      })
      .addCase(addPermission.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 更新权限
      .addCase(modifyPermission.pending, (state) => {
        state.loading = true
      })
      .addCase(modifyPermission.fulfilled, (state) => {
        state.loading = false
      })
      .addCase(modifyPermission.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 删除权限
      .addCase(removePermission.pending, (state) => {
        state.loading = true
      })
      .addCase(removePermission.fulfilled, (state, action) => {
        state.loading = false
        state.permissionList = state.permissionList.filter((item) => item.id !== action.payload.id)
      })
      .addCase(removePermission.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
  },
})

export const { clearError, clearCurrentPermission } = permissionSlice.actions
export default permissionSlice.reducer

